package com.study.design.pattern.pattern.behavioral.observer.java;

/**
 * @Author: gongsl
 * @Date: 2025-03-23 22:51
 */
public class ObserverPatternTest {
    public static void main(String[] args) {
        // 创建两个独立的被观察者
        WeatherStation weatherStation = new WeatherStation();
        StockMarket stockMarket = new StockMarket();

        // 创建各自的观察者
        WeatherReporter weatherReporter = new WeatherReporter();
        StockAnalyst stockAnalyst = new StockAnalyst();

        // 注册观察者
        weatherStation.addPropertyChangeListener(weatherReporter); // 天气记者观察天气
        stockMarket.addPropertyChangeListener(stockAnalyst); // 股票分析师观察股市

        // 执行操作
        System.out.println("\n--- WeatherStation operations ---");
        weatherStation.setTemperature(30.5);
        weatherStation.setTemperature(32.0);

        // 移除天气记者
        System.out.println("\n>>> 移除 WeatherReporter <<<");
        weatherStation.removePropertyChangeListener(weatherReporter);

        // 观察者被移除后，再次更新温度
        weatherStation.setTemperature(35.0); // 这次不会通知 WeatherReporter

        System.out.println("\n--- StockMarket operations ---");
        stockMarket.setStockPrice(150.75);
        stockMarket.setStockPrice(155.50);
    }
}
